package com.vichat.room.controller;

import com.vichat.common.cache.LocalCacher;
import com.vichat.common.constant.GlobalConstant;
import com.vichat.common.constant.I18nConstant;
import com.vichat.common.exception.ViChatException;
import com.vichat.common.util.I18NUtil;
import com.vichat.common.util.NumberUtils;
import com.vichat.common.vo.Grid;
import com.vichat.common.vo.Json;
import com.vichat.common.vo.PageFilter;
import com.vichat.common.vo.SysMsg;
import com.vichat.room.cache.*;
import com.vichat.room.entity.RoomBasic;
import com.vichat.room.entity.RoomMember;
import com.vichat.room.entity.TypedefWindowId;
import com.vichat.room.service.IRoomService;
import com.vichat.room.vo.RoomVO;
import com.vichat.user.controller.BaseController;
import com.vichat.user.entity.UserBasic;
import com.vichat.user.entity.UserOrg;
import com.vichat.user.service.IStaticDataService;
import com.vichat.user.service.IUserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by ranjx on 2017/10/18.
 */
@Controller
@RequestMapping("/vcapi/room")
public class RoomController extends BaseController {
    ExecutorService tp = Executors.newCachedThreadPool();
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IStaticDataService staticDataService;


    /**
     * 查询频道信息
     *
     * @param queryBean
     * @param pf
     * @return
     */
    @RequestMapping("/queryRooms")
    @ResponseBody
    public Grid queryRooms(RoomBasic queryBean, PageFilter pf) {
        Grid grid = new Grid();
        try {
            UserBasic sessionUser = getSessionUser();
            if (sessionUser != null) {
                queryBean.setPartyId(sessionUser.getPartyId());
                if (NumberUtils.isNullOrZero(queryBean.getOrgId())) {
                    queryBean.setOrgId(sessionUser.getOrgId());
                }
            }
            if (StringUtils.isNotBlank(queryBean.getName())) {
                queryBean.setName(URLDecoder.decode(queryBean.getName(), "UTF-8"));
                if (queryBean.getName().indexOf("_") >= 0) {
                    queryBean.setName(queryBean.getName().replaceAll("_", "\\\\_"));
                }
            }
            List<RoomBasic> rooms = roomService.queryRoomBasics(queryBean, pf);
            if (!rooms.isEmpty()) {
                for (RoomBasic room : rooms) {
                    UserOrg orgRedis = userService.queryUserOrgByOrgId(room.getOrgId());
                    room.setOrgName(orgRedis.getOrgName());
                    if(NumberUtils.isNotNullOrZero(room.getOwnerId())) {
                        UserBasic userFDB = userService.getUserBasicByUid(room.getOwnerId());
                        if (userFDB != null) {
                            room.setOwnerName(userFDB.getUserName());
                        }
                    }
                }
            }
            long count = roomService.countRoomBasics(queryBean);
            grid.setData(rooms);
            grid.setCount(count);
        } catch (Exception e) {
            e.printStackTrace();
            grid.setMsg(e.getMessage());
            grid.setCode(-9);
        }
        return grid;
    }

    /**
     * 保存或修改频道信息
     *
     * @param room
     * @return
     */
    @RequestMapping("/saveRoom")
    @ResponseBody
    public Json saveRoom(@RequestBody RoomBasic room) {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            room.setPartyId(sessionUser.getPartyId());
            room.setOrgId(sessionUser.getOrgId());
            room.setOrgCode(sessionUser.getOrgCode());
            room.setOwnerId(sessionUser.getUid());

            //频道初始化
            initRoomBasic(room);

            room = roomService.saveRoomBasic(room);
            roomService.updateRoomBasicVersion(room.getRid());
            json.setSuccess(true);
        } catch (ViChatException e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setI18nCode(e.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 保存或修改频道窗口信息
     *
     * @param room
     * @return
     */
    @RequestMapping("/saveOrUpdRoomWin")
    @ResponseBody
    public Json saveOrUpdRoomWin(RoomBasic room) {
        Json json = new Json();
        try {
            RoomBasic roomBasic = roomService.saveRoomWin(room);
            roomService.updateRoomBasicVersion(roomBasic.getRid());
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 删除频道信息
     *
     * @param rid
     * @return
     */
    @RequestMapping("/deleteRoom")
    @ResponseBody
    public Json deleteRoom(Long rid) {
        Json json = new Json();
        try {
            UserBasic userBasic = getSessionUser();
            RoomBasic roomBasicFDB = roomService.getRoomBasicByRid(rid);
            if (roomBasicFDB != null && roomBasicFDB.getPartyId().longValue() == userBasic.getPartyId().longValue()) {
                roomBasicFDB = roomService.delRoomBasic(rid);
                roomService.updateRoomBasicVersion(rid);
                json.setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 查询频道成员信息
     *
     * @param queryBean
     * @param pf
     * @return
     */
    @RequestMapping("/queryRoomMembers")
    @ResponseBody
    public Grid queryRoomMembers(RoomMember queryBean, PageFilter pf) {
        Grid grid = new Grid();
        try {
            if (StringUtils.isNotBlank(queryBean.getUserName())) {
                queryBean.setUserName(URLDecoder.decode(queryBean.getUserName(), "UTF-8"));
                if (queryBean.getUserName().indexOf("_") >= 0) {
                    queryBean.setUserName(queryBean.getUserName().replaceAll("_", "\\\\_"));
                }
            }
            List<RoomMember> roomMembers = roomService.queryRoomMemers(queryBean, pf);
            long count = roomService.countRoomMembers(queryBean);
            grid.setData(roomMembers);
            grid.setCount(count);
        } catch (Exception e) {
            e.printStackTrace();
            grid.setMsg(e.getMessage());
            grid.setCode(-9);
        }
        return grid;
    }


    /**
     * 添加频道用户
     *
     * @param queryParam
     * @return
     */
    @RequestMapping("/saveRoomMembers")
    @ResponseBody
    public Json saveRoomMembers(@RequestBody RoomMember queryParam) {
        Json json = new Json();
        try {
            long rid = queryParam.getRid();
            RoomBasic roomBasic = roomService.getRoomBasicByRid(rid);
            UserBasic userBasic = getSessionUser();
            if (roomBasic != null && roomBasic.getPartyId().longValue() == userBasic.getPartyId().longValue()) {//登录用户和频道是一个企业下的
                if (queryParam.getUids() != null && queryParam.getUids().size() > 0) {
                    for (long uid : queryParam.getUids()) {
                        if (uid > 0) {
                            RoomMember hasMember = roomService.getRoomMember(queryParam.getRid(), uid);
                            if (hasMember != null) {
                                hasMember.setAdminGrade(queryParam.getAdminGrade());
                                hasMember.setPriority(queryParam.getPriority());
                                hasMember.setTimelen(queryParam.getTimelen());
                                hasMember.setVideolen(queryParam.getVideolen());
                                roomService.saveRoomMember(hasMember);
                            } else {
                                UserBasic userFDB = userService.getUserBasicByUid(uid);
                                if (userFDB.getPartyId().longValue() == userBasic.getPartyId().longValue()) {//成员和登录用户也必须是一个企业下
                                    RoomMember member = new RoomMember();
                                    member.setRid(queryParam.getRid());
                                    member.setUid(uid);
                                    member.setAdminGrade(queryParam.getAdminGrade());
                                    member.setPriority(queryParam.getPriority());
                                    member.setTimelen(queryParam.getTimelen());
                                    member.setVideolen(queryParam.getVideolen());
                                    member.setUserName(userFDB.getUserName());
                                    roomService.saveRoomMember(member);
                                }
                            }

                            roomService.updateRoomMemberVersion(uid,rid);
                        }
                    }
                }
            }
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    /**
     * 删除成员(修改频道成员的权限为0，逻辑删除)
     *
     * @param rid
     * @return
     */
    @RequestMapping("/deleteRoomMember")
    @ResponseBody
    public Json deleteRoomMember(long rid, long uid) {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            UserBasic userFDB = userService.getUserBasicByUid(uid);
            if (userFDB.getPartyId().longValue() == sessionUser.getPartyId().longValue()) {
                roomService.delRoomMemberLogically(rid, uid);
                roomService.updateRoomMemberVersion(uid,rid);
                json.setSuccess(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 批量删除用户信息
     * batchDelUser
     *
     * @param roomMembers
     * @return
     */
    @RequestMapping("/batchDeleteRoomMember")
    @ResponseBody
    public Json batchDeleteRoomMember(@RequestBody List<RoomMember> roomMembers) {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();
            if (!roomMembers.isEmpty()) {
                long rid = roomMembers.get(0).getRid();
                for (RoomMember roomMember : roomMembers) {
                    UserBasic userFDB = userService.getUserBasicByUid(roomMember.getUid());
                    if (userFDB.getPartyId().longValue() == sessionUser.getPartyId().longValue()) {
                        roomService.delRoomMemberLogically(roomMember.getRid(), roomMember.getUid());
                        roomService.updateRoomMemberVersion(userFDB.getUid(),rid);
                    }
                }
            }
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    /**
     * 获取频道视频窗口
     *
     * @param rid
     * @return
     */
    @RequestMapping("/queryTypedefWindowIds")
    @ResponseBody
    public Grid queryTypedefWindowIds(long rid) {
        Grid grid = new Grid();
        try {
            List<TypedefWindowId> windowIds = new ArrayList<>();
            RoomBasic roomBasicFDB = roomService.getRoomBasicByRid(rid);
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin1())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin1().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(1);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin2())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin2().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(2);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin3())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin3().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(3);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin4())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin4().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(4);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin5())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin5().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(5);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin6())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin6().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(6);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin7())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin7().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(7);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin8())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin8().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(8);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin9())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin9().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(9);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin10())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin10().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(10);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin11())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin11().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(11);
                windowIds.add(typedefWindowId);
            }
            if (NumberUtils.isNotNullOrZero(roomBasicFDB.getWin12())) {
                TypedefWindowId temp = LocalCacher.get("TYPEDEF_WINDOW_ID", "Type_WindowId", roomBasicFDB.getWin12().toString(), TypedefWindowId.class);
                TypedefWindowId typedefWindowId = new TypedefWindowId();
                BeanUtils.copyProperties(typedefWindowId, temp);
                typedefWindowId.setWinNum(12);
                windowIds.add(typedefWindowId);
            }
            grid.setData(windowIds);
        } catch (Exception e) {
            e.printStackTrace();
            grid.setMsg(e.getMessage());
            grid.setCode(-9);
        }
        return grid;
    }

    /**
     * 发送频道消息
     *
     * @param sysMsgs
     * @return
     */
    @RequestMapping("/sendSysMsg")
    @ResponseBody
    public Json sendSysMsg(@RequestBody List<SysMsg> sysMsgs) {
        Json json = new Json();
        try {
            if (NumberUtils.isNotNullOrZero(sysMsgs.size())) {
                for (SysMsg sysMsg : sysMsgs) {
                    sysMsg.setCmdType(GlobalConstant.GMS_EVENT_QUEUE);
                    sysMsg.setMsgType(GlobalConstant.GMS_MSG_TYPE_2000);
                    sysMsg.setSndId(10000);
                }
                roomService.sendSysMsg(sysMsgs);
            }
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * loadRoomCache 刷新房间配置
     *
     * @return
     */
    @RequestMapping("/refreshRoomCache")
    @ResponseBody
    public Json refreshRoomCache() {
        Json json = new Json();
        try {
            LocalCacher.refresh(TypedefAudioIdCache.cacheId());
            LocalCacher.refresh(TypedefEnterCtrlCache.cacheId());
            LocalCacher.refresh(TypedefMicModCache.cacheId());
            LocalCacher.refresh(TypedefPttCtrlCache.cacheId());
            LocalCacher.refresh(TypedefStyleIdCache.cacheId());
            LocalCacher.refresh(TypedefVidoIdCache.cacheId());
            LocalCacher.refresh(TypedefWindowIdCache.cacheId());

            json = this.loadRoomCache();

            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    /**
     * loadRoomCache 加载房间配置
     *
     * @return
     */
    @RequestMapping("/loadRoomCache")
    @ResponseBody
    public Json loadRoomCache() {
        Json json = new Json();
        try {
            List<Object> roomCache = new ArrayList<>();
            roomCache.add(LocalCacher.getAll(TypedefAudioIdCache.cacheId()));
            roomCache.add(LocalCacher.getAll(TypedefEnterCtrlCache.cacheId()));
            roomCache.add(LocalCacher.getAll(TypedefMicModCache.cacheId()));
            roomCache.add(LocalCacher.getAll(TypedefPttCtrlCache.cacheId()));
            roomCache.add(LocalCacher.getAll(TypedefStyleIdCache.cacheId()));
            roomCache.add(LocalCacher.getAll(TypedefVidoIdCache.cacheId()));
            roomCache.add(LocalCacher.getAll(TypedefWindowIdCache.cacheId()));
            json.setObj(roomCache);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


    /**
     * 根据频道名称模糊查询频道成员
     *
     * @param userName
     * @return
     */
    @RequestMapping("/getRoomMembersByUserName")
    @ResponseBody
    public Json getRoomMembersByUserName(String userName) {
        Json json = new Json();
        try {
            if (StringUtils.isNotBlank(userName)) {
                userName = URLDecoder.decode(userName, "UTF-8");
            }
            List<RoomMember> roomMembers = roomService.getRoomMembersByUserName(userName);
            json.setObj(roomMembers);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }


//    /**
//     * 组呼
//     *
//     * @param uids
//     * @return
//     */
//    @RequestMapping("/groupCall")
//    @ResponseBody
//    public Json groupCall(@RequestParam("uids[]") String[] uids) {
//        Json json = new Json();
//        try {
//            UserBasic sessionUser = getSessionUser();
//            List<SysStaticData> sysStaticDatas = staticDataService.getStaticData("Dispartch:" + sessionUser.getPartyId());
//            if (sysStaticDatas.isEmpty()) {
//                throw new Exception("请在系统参数中配置调度台手机号码，枚举KEY=Dispartch:" + sessionUser.getPartyId() + ",枚举VALUE=调度台手机号码");
//            }
//            String phone = sysStaticDatas.get(0).getCodeValue();
//            UserBasic userBasicFDB = userService.getUserBasicByPhone(phone);
//            if (userBasicFDB == null) {
//                throw new Exception("调度台手机号码不存在，请重新配置");
//            }
//            RoomVO roomVO = new RoomVO();
//            roomVO.setStyle(1l);
//            roomVO.setExpireType(0l);
//            long[] uuids = (long[]) ConvertUtils.convert(uids, long.class);
//            ArrayList<Long> senduids = new ArrayList<>();
//            int seq = 0;
//            String name = userBasicFDB.getUserName();
//            for (long uid : uuids) {
//                if (seq < 5) {
//                    UserBasic userFDB = userService.getUserBasicByUid(uid);
//                    if (userFDB != null && !name.contains(userFDB.getUserName())) {
//                        name += "," + userFDB.getUserName();
//                        senduids.add(uid);
//                    }
//                    seq++;
//                }
//            }
//            senduids.add(userBasicFDB.getUid());
//            roomVO.setUids(senduids);
//            roomVO.setName(name);
//            roomVO.setUid(sessionUser.getUid());
//            roomVO.setPartyId(sessionUser.getPartyId());
//            roomVO.setOrgId(sessionUser.getOrgId());
//            roomVO.setOrgCode(sessionUser.getOrgCode());
//            roomVO.setHour(12);
//            RoomBasic roomBasicFDB = roomService.createRoom2Member(roomVO);
//            if (roomBasicFDB != null) {
//                //刷新频道
//                tp.execute(new RefreshRoomThread(roomService, roomBasicFDB.getRid(), roomBasicFDB.getName(), false));
//            }
//            json.setSuccess(true);
//        } catch (Exception e) {
//            json.setMsg(e.getMessage());
//            json.setCode(-9);
//        }
//        return json;
//    }

    protected RoomBasic initRoomBasic(RoomBasic roomBasic) {
        roomBasic.setExpireType(1L);
        roomBasic.setLoginType(1L);
        roomBasic.setEnterCtrl(1L);
        roomBasic.setOfflineCtrl(1L);
        roomBasic.setCapacity(1000L);
        if (roomBasic.getStyle().longValue() == GlobalConstant.ROOM_STYLE_1) {//语音
            roomBasic.setWin1(100L);//无视频|16k语音|自动麦
            roomBasic.setWin2(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin3(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin4(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin5(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin6(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin7(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin8(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin9(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin10(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin11(0L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin12(0L);//视频窗口640x480码率300k|音频16khz|自动麦
        } else if (roomBasic.getStyle().longValue() == GlobalConstant.ROOM_STYLE_3) {//视频对讲
            roomBasic.setWin1(100L);//无视频|16k语音|自动麦
            roomBasic.setWin2(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin3(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin4(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin5(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin6(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin7(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin8(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin9(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin10(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin11(15L);//视频窗口640x480码率300k|音频16khz|自动麦
            roomBasic.setWin12(0L);//视频窗口640x480码率300k|音频16khz|自动麦
        }
        return roomBasic;
    }

    /**
     * 设置频道管理员
     *
     * @param uid
     * @param rid
     * @return
     */
    @RequestMapping("/setRoomMemberAdminGrade")
    @ResponseBody
    public Json setRoomMemberAdminGrade(Long uid, Long rid, Long adminGrade) {
        Json json = new Json();
        try {
            roomService.setRoomMemberAdminGrade(uid, rid, adminGrade);
            roomService.updateRoomMemberVersion(uid,rid);
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }

    /**
     * 创建频道
     *
     * @param roomVO
     * @return
     */
    @RequestMapping("/createRoom")
    @ResponseBody
    public Json createRoom(@RequestBody RoomVO roomVO) {
        Json json = new Json();
        try {
            UserBasic sessionUser = getSessionUser();

            if (StringUtils.isBlank(roomVO.getName())) {
//                throw new Exception("频道名称不能为空");
                String errorMsg = I18NUtil.getMessage("RoomApiController.createRoom.error_roomNameNotEmpty", null, Locale.getDefault());
                throw new ViChatException(I18nConstant.error_roomNameNotEmpty, errorMsg);
            }
            if (NumberUtils.isNullOrZero(roomVO.getStyle())) {
//                throw new Exception("频道类型不能为空");
                String errorMsg = I18NUtil.getMessage("RoomApiController.createRoom.error_roomTypeNotEmpty", null, Locale.getDefault());
                throw new ViChatException(I18nConstant.error_roomTypeNotEmpty, errorMsg);
            }
            if (NumberUtils.isNull(roomVO.getExpireType())) {
//                throw new Exception("频道有效期不能为空");
                String errorMsg = I18NUtil.getMessage("RoomApiController.createRoom.error_roomExpireDateNotEmpty", null, Locale.getDefault());
                throw new ViChatException(I18nConstant.error_expireDateNotEmpty, errorMsg);
            }
            if (roomVO.getUids().isEmpty()) {
//                throw new Exception("频道成员不能为空");
                String errorMsg = I18NUtil.getMessage("RoomApiController.createRoom.error_uidsNotEmpty", null, Locale.getDefault());
                throw new ViChatException(I18nConstant.error_roomUidsNotEmpty, errorMsg);
            }
//            if (NumberUtils.isNullOrZero(roomVO.getUid())) {
////                throw new Exception("用户UID不能为空");
//                String errorMsg = I18NUtil.getMessage("RoomApiController.createRoom.error_uidNotEmpty", null, Locale.getDefault());
//                throw new ViChatException(I18nConstant.error_uIdNotEmpty, errorMsg);
//            }
//            UserBasic userBasic = userService.getUserBasicByUid(roomVO.getUid());
//            if (userBasic == null) {
////                throw new Exception("当前民警信息不存在");
//                String errorMsg = I18NUtil.getMessage("RoomApiController.createRoom.error_userNotExist",null,Locale.getDefault());
//                throw new ViChatException(I18nConstant.error_userNotExist,errorMsg);
//            }
            roomVO.setPartyId(sessionUser.getPartyId());
            roomVO.setOrgId(sessionUser.getOrgId());
            roomVO.setOrgCode(sessionUser.getOrgCode());
            roomVO.setUid(sessionUser.getUid());
            RoomBasic roomBasicFDB = roomService.createRoom2Member(roomVO);
            if (roomBasicFDB != null) {
                roomService.updateRoomBasicVersion(roomBasicFDB.getRid());

                if (!roomVO.getUids().isEmpty() && roomBasicFDB != null) {
                    for (long uid : roomVO.getUids()) {
                        roomService.updateRoomMemberVersion(uid,roomBasicFDB.getRid());
                    }
                }
            }
            HashMap map = new HashMap();
            map.put("rid", roomBasicFDB.getRid());
            map.put("name", roomBasicFDB.getName());
            json.setObj(map);
            json.setSuccess(true);
        } catch (ViChatException e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setI18nCode(e.getCode());
            json.setCode(-9);
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            json.setCode(-9);
        }
        return json;
    }
}
